MODE

Overview

The MODE function returns the modal value—the most frequently occurring value—in a dataset. The mode is one of the fundamental measures of central tendency in descriptive statistics, alongside the mean and median. Unlike the mean, the mode is particularly useful for categorical data and distributions with repeated values.

This implementation wraps the scipy.stats.mode function from the SciPy scientific computing library. The underlying algorithm uses numpy.unique to count the frequency of each distinct value in the input array. For more details, see the SciPy source code on GitHub.

The function flattens multi-dimensional input into a single array and filters out any non-numeric values before computing the mode. When multiple values share the highest frequency (a multimodal distribution), the function returns the smallest value among them—a convention that ensures deterministic output. If no value appears more than once, the function returns an error since a mode requires at least two occurrences of the same value.

Mathematically, for a dataset with values x_1, x_2, \ldots, x_n, the mode is defined as:

\text{mode} = \arg\max_{x} \, f(x)

where f(x) represents the frequency (count) of value x in the dataset.

The nan_policy='omit' parameter is used internally, meaning NaN values are excluded from the calculation rather than propagating through the result.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=MODE(data)
  • data (list[list], required): 2D array of numeric values to find the mode of. Non-numeric values are ignored.

Returns (float): The most common value in the data, or str error message if invalid.

Examples

Example 1: Simple mode with repeated value

Inputs:

data
1 2
2 3
2 4

Excel formula:

=MODE({1,2;2,3;2,4})

Expected output:

2

Example 2: Multiple modes returns smallest

Inputs:

data
1 2
2 1
3 3

Excel formula:

=MODE({1,2;2,1;3,3})

Expected output:

1

Example 3: Non-numeric values ignored

Inputs:

data
1 a
2 2
2 b

Excel formula:

=MODE({1,"a";2,2;2,"b"})

Expected output:

2

Example 4: Mode of a larger array

Inputs:

data
10 20 30
20 40 50
60 20 80

Excel formula:

=MODE({10,20,30;20,40,50;60,20,80})

Expected output:

20

Python Code

from scipy.stats import mode as scipy_mode

def mode(data):
    """
    Returns the modal (most common) value in the passed array. Wraps scipy.stats.mode to flatten the input, ignore non-numeric values, and always return a single mode (the smallest if multiple). If no mode is found (all values occur only once), returns an error.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.mode.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): 2D array of numeric values to find the mode of. Non-numeric values are ignored.

    Returns:
        float: The most common value in the data, or str error message if invalid.
    """
    # Helper function to normalize scalar input to 2D list
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    # Normalize input for 2D list args
    data = to2d(data)

    # If input is not a list of lists, return error
    if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
        return "Invalid input: data must be a 2D list."
    # Flatten and filter numeric values
    flat = []
    for row in data:
        for val in row:
            try:
                v = float(val)
                flat.append(v)
            except (ValueError, TypeError):
                continue
    if len(flat) < 2:
        return "Invalid input: data must contain at least two numeric values."
    # Use scipy.stats.mode
    res = scipy_mode(flat, keepdims=False, axis=None, nan_policy='omit')
    if res.count < 2:
        return "Error: No mode found. At least one value must appear more than once."
    # Return as Python float scalar, not numpy type
    mode_value = float(res.mode.item()) if hasattr(res.mode, 'item') else float(res.mode)
    return mode_value

Online Calculator